package com.yyk.yykywd.ui.work;

import android.content.Intent;
import android.os.Bundle;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.orhanobut.hawk.Hawk;
import com.orhanobut.logger.Logger;
import com.yyk.yykywd.R;
import com.yyk.yykywd.base.BaseActivity;
import com.yyk.yykywd.base.BaseRecyclerAdapter;
import com.yyk.yykywd.base.BaseRecyclerHolder;
import com.yyk.yykywd.bean.WorkPlandetailInfo;
import com.yyk.yykywd.config.Constant;
import com.yyk.yykywd.config.HttpUrl;
import com.yyk.yykywd.http.ApiService;
import com.yyk.yykywd.ui.WebActivity;
import com.yyk.yykywd.util.Md5Util2;
import com.yyk.yykywd.util.OnClickFlagUtil;
import com.yyk.yykywd.widght.diver.more.SpacesItemDecoration;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 合作成功详情
 */
public class CooperationCgDetailActivity extends BaseActivity {

    @BindView(R.id.tv_number)
    TextView tvNumber;
    @BindView(R.id.tv_createtime)
    TextView tvCreatetime;
    @BindView(R.id.tv_topname)
    TextView tvTopname;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_time_end)
    TextView tvTimeEnd;
    @BindView(R.id.tv_goal)
    TextView tvGoal;
    @BindView(R.id.tv_rests)
    TextView tvRests;
    @BindView(R.id.rv_udate)
    RecyclerView rvUdate;
    @BindView(R.id.tv_createtime2)
    TextView tvCreatetime2;
    @BindView(R.id.tv_name2)
    TextView tvName2;
    @BindView(R.id.tv_phone2)
    TextView tvPhone2;
    @BindView(R.id.tv_postname2)
    TextView tvPostname2;
    @BindView(R.id.tv_describe2)
    TextView tvDescribe2;
    @BindView(R.id.tv_remark2)
    TextView tvRemark2;
    @BindView(R.id.rv_udate3)
    RecyclerView rvUdate3;
    @BindView(R.id.tv_remark3)
    TextView tvRemark3;
    @BindView(R.id.tv_number4)
    TextView tvNumber4;
    @BindView(R.id.tv_money4)
    TextView tvMoney4;
    @BindView(R.id.rv_udate4)
    RecyclerView rvUdate4;
    @BindView(R.id.tv_remark4)
    TextView tvRemark4;
    @BindView(R.id.tv_classes5)
    TextView tvClasses5;
    @BindView(R.id.tv_money5)
    TextView tvMoney5;
    @BindView(R.id.tv_paymoney5)
    TextView tvPaymoney5;
    @BindView(R.id.tv_remark5)
    TextView tvRemark5;
    @BindView(R.id.tv_createtime3)
    TextView tvCreatetime3;
    @BindView(R.id.tv_createtime4)
    TextView tvCreatetime4;
    @BindView(R.id.ll_ywgzlc)
    LinearLayout llYwgzlc;
    @BindView(R.id.ll_money5) //回款判断
            LinearLayout llMoney5;
    @BindView(R.id.ll_paymoeny5)
    LinearLayout llPaymoeny5;
    private String id;

    @Override
    public int getLayoutId() {
        return R.layout.activity_cooperation_cg_detail;
    }

    @Override
    public void initView() {
        initToolbar();
    }


    private void initToolbar() {
        setBackArrow();
        setTitle("合作成功");
    }

    @Override
    public void initData() {
        initGetIntent();
        postWorkPlandetail(true);

    }

    private void initGetIntent() {
        if (getBundle() != null) {
            id = getBundle().getString("id");
        }
    }

    /**
     * 接口4：查看业务详情（whm）
     *
     * @param flag
     */
    private void postWorkPlandetail(final boolean flag) {
        final Map<String, String> map = new TreeMap<>();
        map.put("uid", Hawk.get("uid")+"");
        map.put("bid", id + "");
        map.put("sign", Md5Util2.createSign(map));
        ApiService.getInstance().api.postWorkPlandetail(map)
                .subscribeOn(Schedulers.io())     //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())   //取消订阅
                .subscribe(new Observer<WorkPlandetailInfo>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(WorkPlandetailInfo info) {
                        Logger.e("查看业务详情" + info.toString());
                        if (info.getCode() == 1) {
                            //1.业务计划
                            WorkPlandetailInfo.DataBean.PlanBean plan = info.getData().getPlan();

                            //业务编号
                            String number = plan.getNumber();
                            //创建时间
                            String createtime = plan.getCreatetime();
                            //业务类型
                            String topname = plan.getTopname();
                            //机构名称
                            String name = plan.getName();
                            tvCreatetime.setText(createtime);
                            tvGoal.setText(plan.getGoal());
                            tvNumber.setText(number);
                            tvRests.setText(plan.getRests());
                            if (plan.getEnd() == 1) {
                                tvTimeEnd.setText(plan.getTime() + "   上午");
                            } else if (plan.getEnd() == 2) {
                                tvTimeEnd.setText(plan.getTime() + "   下午");
                            } else {
                                tvTimeEnd.setText(plan.getTime() + "   晚上");
                            }
                            tvTopname.setText(topname);
                            tvName.setText(name);
                            List<WorkPlandetailInfo.DataBean.PlanBean.UdataBean> udata = plan.getUdata();
                            BaseRecyclerAdapter<WorkPlandetailInfo.DataBean.PlanBean.UdataBean> adapter = new BaseRecyclerAdapter<WorkPlandetailInfo.DataBean.PlanBean.UdataBean>(mActivity, udata, R.layout.adapter_item_businessplan_deteail) {
                                @Override
                                public void convert(BaseRecyclerHolder holder, WorkPlandetailInfo.DataBean.PlanBean.UdataBean item, int position, boolean isScrolling) {
                                    holder.setImageByUrl(R.id.iv_img, item.getImg());
                                    holder.setText(R.id.tv_uname, item.getUname());
                                }
                            };
                            rvUdate.setLayoutManager(new LinearLayoutManager(mActivity, LinearLayoutManager.HORIZONTAL, false));
                            rvUdate.setAdapter(adapter);
                            //2.拜访信息
                            WorkPlandetailInfo.DataBean.VisitBean visit = info.getData().getVisit();
                            tvCreatetime2.setText(visit.getCreatetime());
                            tvName2.setText(visit.getName());
                            tvPhone2.setText(visit.getPhone());
                            tvDescribe2.setText(visit.getDescribe());
                            tvPostname2.setText(visit.getPost_name());
                            tvRemark2.setText(visit.getRemark());
                            //3.项目申请书信息
                            WorkPlandetailInfo.DataBean.ApplyBean apply = info.getData().getApply();
                            tvCreatetime3.setText(apply.getCreatetime());
                            List<String> img3 = apply.getImg();//图片
                            String remark3 = apply.getRemark();//备注
                            tvRemark3.setText(remark3);
                            BaseRecyclerAdapter<String> adapter3 = new BaseRecyclerAdapter<String>(mActivity, img3, R.layout.adapter_item_shenqingimg_deteailb) {
                                @Override
                                public void convert(BaseRecyclerHolder holder, String item, int position, boolean isScrolling) {
                                    holder.setImageByUrl(R.id.iv_img, item);
                                }
                            };
                            rvUdate3.setLayoutManager(new GridLayoutManager(mActivity, 4, GridLayoutManager.VERTICAL, false));
                            rvUdate3.addItemDecoration(new SpacesItemDecoration(14, 0));
                            rvUdate3.setAdapter(adapter3);
                            //4.合同信息
                            WorkPlandetailInfo.DataBean.PactBean pact = info.getData().getPact();
                            tvCreatetime4.setText(pact.getCreatetime());
                            List<String> img4 = pact.getImg();
                            String remark4 = apply.getRemark();//备注
                            tvRemark4.setText(remark4);
                            BaseRecyclerAdapter<String> adapter4 = new BaseRecyclerAdapter<String>(mActivity, img4, R.layout.adapter_item_shenqingimg_deteailb) {
                                @Override
                                public void convert(BaseRecyclerHolder holder, String item, int position, boolean isScrolling) {
                                    holder.setImageByUrl(R.id.iv_img, item);
                                }
                            };
                            rvUdate4.setLayoutManager(new GridLayoutManager(mActivity, 4, GridLayoutManager.VERTICAL, false));
                            rvUdate4.addItemDecoration(new SpacesItemDecoration(14, 0));
                            rvUdate4.setAdapter(adapter4);
                            String money4 = pact.getMoney();
                            String number4 = pact.getNumber();
                            tvNumber4.setText(number4);
                            tvMoney4.setText(money4 + "元");
                            //5.回款信息
                            WorkPlandetailInfo.DataBean.ReturnedBean returned = info.getData().getReturned();
                            tvMoney5.setText(returned.getMoney() + "元");
                            tvPaymoney5.setText(returned.getPay_money() + "元");
                            tvRemark5.setText(returned.getRemark());
                            int classes = returned.getClasses();
                            if (classes == 1) {
                                tvClasses5.setText("全部回款");
                                llPaymoeny5.setVisibility(View.GONE);
                            } else if (classes == 2) {
                                tvClasses5.setText("部分回款");
                            } else {
                                tvClasses5.setText("不需回款");
                                llPaymoeny5.setVisibility(View.GONE);
                                llMoney5.setVisibility(View.GONE);
                            }


                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


    @OnClick(R.id.ll_ywgzlc)
    public void onViewClicked() {
        if (OnClickFlagUtil.isFastClick()) {
            return;
        }
        Intent intent = new Intent();
        //业务跟踪流程
        intent.putExtra("url", HttpUrl.BASE_H5_URL + "business/?uid=" + Hawk.get("uid") + "&did=" + id);
        startAct(intent, WebActivity.class, null);
    }


}
